package com.lala.service.impl;

import com.lala.dao.ClientMessageDao;
import com.lala.entity.ClientLogin;
import com.lala.dao.ClientLoginDao;
import com.lala.entity.ClientMessage;
import com.lala.exception.LoginException;
import com.lala.service.ClientLoginService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * (ClientLogin)表服务实现类
 *
 * @author makejava
 * @since 2020-03-24 11:16:13
 */
@Service("clientLoginService")
public class ClientLoginServiceImpl implements ClientLoginService {


    @Resource
    private ClientLoginDao clientLoginDao;
    @Resource
    private ClientMessageDao clientMessageDao;

    /**
     * @author 霍明铭
     * @param userName
     * @describe 根据用户名查询用户
     */
    @Override
    public ClientLogin seachexit(String userName) {
        return clientLoginDao.seachexit(userName);
    }
    /**
     * @author 霍明铭
     * @describe 增加用户
     */
    @Override
    public void save(ClientLogin clientLogin, Integer tel, String mail) {
        String uuid= UUID.randomUUID().toString();
       /* Md5Hash md5Hash=new Md5Hash(clientLogin.getPassWord(),uuid,1024);
        clientLogin.setPassWord(md5Hash.toString());*/
        clientLogin.setPwdSalt(uuid);
        clientLoginDao.insert(clientLogin);
        ClientMessage clientMessage=new ClientMessage();
        clientMessage.setTel(tel);
        clientMessage.setMail(mail);
        clientMessage.setLoginId(clientLogin.getId());
        clientMessageDao.insert(clientMessage);
    }
    /**
     * @author 霍明铭
     * @describe 根据用户名和密码查询用户
     */
    @Override
    public ClientLogin getCLByNameAndPwd(ClientLogin clientLogin) {
        if (clientLogin.getUserName()==null){
            throw new LoginException("用户名不能为空");
        }
        if (clientLogin.getPassWord()==null){
            throw new LoginException("密码不能为空");
        }
      ClientLogin login= clientLoginDao.getCLByNameAndPwd(clientLogin.getUserName(),clientLogin.getPassWord());
        if (login==null){
            throw new LoginException("用户名或密码错误");
        }
        return login;

    }

    /**
     * @author 霍明铭
     * @param mail
     * @param newPassWord
     * 根据邮箱找回密码
     */
    @Override
    public void getPassWordAndPwd(String mail, String newPassWord) {
           ClientLogin clientLogin=clientLoginDao.getCLBymail(mail);
           if(clientLogin==null){
               throw new LoginException("邮箱不存在");
           }
           clientLogin.setPassWord(newPassWord);
           clientLoginDao.update(clientLogin);
    }

    /**
     * 通过ID查询单条数据
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ClientLogin queryById(Integer id) {
        return this.clientLoginDao.queryById(id);
    }
    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<ClientLogin> queryAllByLimit(int offset, int limit) {
        return this.clientLoginDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param clientLogin 实例对象
     * @return 实例对象
     */
    @Override
    public ClientLogin insert(ClientLogin clientLogin) {
        this.clientLoginDao.insert(clientLogin);
        return clientLogin;
    }

    /**
     * 修改数据
     *
     * @param clientLogin 实例对象
     * @return 实例对象
     */
    @Override
    public ClientLogin update(ClientLogin clientLogin) {
        this.clientLoginDao.update(clientLogin);
        return this.queryById(clientLogin.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.clientLoginDao.deleteById(id) > 0;
    }
}